package com.github.crush0.myblog.controller;

import com.github.crush0.myblog.dao.ArticleMessageMapper;
import com.github.crush0.myblog.dao.MessageMapper;
import com.github.crush0.myblog.entity.ArticleMessage;
import com.github.crush0.myblog.entity.Message;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/message")
public class MessageController  extends BaseController{
    private final MessageMapper messageMapper;
    private final ArticleMessageMapper articleMessageMapper;
    @Autowired
    public MessageController(MessageMapper messageMapper,ArticleMessageMapper articleMessageMapper){
        this.messageMapper = messageMapper;
        this.articleMessageMapper = articleMessageMapper;
    }

    @ApiOperation("新增一条留言")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "message", value = "留言内容", required = true, dataType = "string"),
            @ApiImplicitParam(name = "name", value = "留言人", required = true, dataType = "string"),
            @ApiImplicitParam(name = "email", value = "留言人邮箱", required = true, dataType = "string"),
            @ApiImplicitParam(name = "ip", value = "留言人ip", required = true, dataType = "string")
    })
    @PostMapping("/{message}")
    public String addMessage(@PathVariable String message,String name,String email,String ip){
        Message newMessage = new Message();
        newMessage.setCreate_by(new Date());
        newMessage.setContent(message);
        newMessage.setName(name);
        newMessage.setEmail(email);
        newMessage.setIp(ip);
        messageMapper.insert(newMessage);
        return "success";
    }

    @ApiOperation("删除文章某留言")
    @ApiImplicitParam(name = "id", value = "留言id", required = true, dataType = "Long")
    @DeleteMapping("/article/{id}")
    public String deleteArticleMessage(@PathVariable Long id){
        messageMapper.deleteByPrimaryKey(id);
        return "success";
    }

    @ApiOperation("获取某条评论的信息")
    @ApiImplicitParam(name = "id", value = "留言id", required = true, dataType = "Long")
    @GetMapping("/article/{id}")
    public ArticleMessage getArticleMessageInfo(@PathVariable Long id){
        return articleMessageMapper.selectByPrimaryKey(id);
    }

    @ApiOperation("给文章增加一条评论信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "文章id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "msgId",value = "留言id",required = true,dataType = "string")
    })
    @PostMapping("/article/{id}")
    public String getMessageInfo(@PathVariable Long id,Long msgId){
        ArticleMessage articleMessage = new ArticleMessage();
        articleMessage.setArticle_id(id);
        articleMessage.setIs_effective(true);
        articleMessage.setCreate_by(new Date());
        articleMessage.setMessage_id(msgId);
        articleMessageMapper.insert(articleMessage);
        return "success";
    }

    @ApiOperation("获取所有留言信息")
    @GetMapping("/list")
    public List<Message> getAllMessageInfo(){
        return messageMapper.selectAll();
    }

    @ApiOperation("回复留言")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "留言id", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "message",value = "留言内容",required = true,dataType = "string")
    })
    @GetMapping("/reply/{id}")
    public String replyMessage(@PathVariable Long id, String message, HttpSession session){
        Message fMessage = messageMapper.selectByPrimaryKey(id);
        Message replyMessage = new Message();
        String replyName = fMessage.getIp();
        if(!fMessage.getName().equals("")){
            replyName = fMessage.getName();
        }else if(!fMessage.getEmail().equals("")){
            replyName = fMessage.getEmail();
        }
        replyMessage.setContent("@"+replyName+":"+message);
        replyMessage.setIs_effective(true);
        messageMapper.insert(replyMessage);
        return "success";
    }

    @ApiOperation("删除一条留言")
    @ApiImplicitParam(name = "id", value = "留言id", required = true, dataType = "Long")
    @DeleteMapping("/{id}")
    public String deleteMessage(@PathVariable Long id){
        messageMapper.deleteByPrimaryKey(id);
        return "success";
    }

    @ApiOperation("获取某条评论的信息")
    @ApiImplicitParam(name = "id", value = "留言id", required = true, dataType = "Long")
    @GetMapping("/{id}")
    public Message getMessageInfo(@PathVariable Long id){
        return messageMapper.selectByPrimaryKey(id);
    }
}
